package com.itheima.stock.service.impl;

import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVo;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {
    private final Cache cache;
    private final IdWorkers idWorkers;
    private static final int FOUR = 4;
    private final SysUserMapper sysUserMapper;
    private final PasswordEncoder passwordEncoder;

    public UserServiceImpl(Cache cache, IdWorkers idWorkers, SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder) {
        this.cache = cache;
        this.idWorkers = idWorkers;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public CaptchaVo captcha() {
        String captcha = RandomStringUtils.randomNumeric(FOUR);
        String redisKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, idWorkers.nextStrId());
        cache.set(redisKey,captcha, Constants.CAPTCHA_EXPIRE);
        return CaptchaVo.builder()
                .rkey(redisKey)
                .code(captcha)
                .build();
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        if(ObjectUtils.isEmpty(loginDTO)
                ||ObjectUtils.isEmpty(loginDTO.getUsername())
                ||ObjectUtils.isEmpty(loginDTO.getPassword())
                ||ObjectUtils.isEmpty(loginDTO.getCode())
                ||ObjectUtils.isEmpty(loginDTO.getRkey())){
            Exceptions.cast(CodeEnum.PARAM_IS_NUT_NULL);
        }
        String captchaInRedis = cache.get(loginDTO.getRkey());
        //System.out.println(captchaInRedis);
        if(!StringUtils.equals(captchaInRedis,loginDTO.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        cache.remove(loginDTO.getRkey());
        SysUser user = sysUserMapper.selectByUsername(loginDTO.getUsername(),Constants.SYS_USER_STATUS_NORMAL,Constants.SYS_USER_UN_DELETED);
        if(ObjectUtils.isEmpty(user) ||!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_USERNAME_NOT_EXISTS);
        }
        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .nickName(user.getNickName())
                .phone(user.getPhone())
                .build();
    }
}
